<?php

namespace StudyBuddy\Math;

use \StudyBuddy\Math\ExpressionTexte;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MathBase
 */
class MathBase {

    //put your code here

    /*     * *************************************************************************
     *   copyright            : (C) 2005 by Pascal Brachet - France            *
     *   pbrachet_NOSPAM_xm1math.net (replace _NOSPAM_ by @)                   *
     *   http://www.xm1math.net/phpmathpublisher/                              *
     *                                                                         *
     *   This program is free software; you can redistribute it and/or modify  *
     *   it under the terms of the GNU General Public License as published by  *
     *   the Free Software Foundation; either version 2 of the License, or     *
     *   (at your option) any later version.                                   *
     *                                                                         *
     * ************************************************************************* */

    /*     * ******* HOW TO USE PHPMATHPUBLISHER ****************************
      1) Fix the path to the fonts and the images directory (see PARAMETERS TO MODIFY below)
      2) Include this script in your php page :
      include("mathpublisher.php") ;
      3) Just call the mathfilter($text,$size,$pathtoimg) function in your php page.
      $text is the text with standard html tags and mathematical expressions (defined by the <m>...</m> tag).
      $size is the size of the police used for the formulas.
      $pathtoimg is the relative path between the html pages and the images directory.
      With a simple "echo mathfilter($text,$size,$pathtoimg);", you can display text with mathematical formulas.
      The mathfilter function will replace all the math tags (<m>formula</m>) in $text by <img src=the formula image >.
      Example :
      mathfilter("A math formula : <m>f(x)=sqrt{x}</m>,12,"img/") will return :
      "A math formula : <img src=\"img/math_988.5_903b2b36fc716cfb87ff76a65911a6f0.png\" style=\"vertical-align:-11.5px; display: inline-block ;\" alt=\"f(x)=sqrt{x}\" title=\"f(x)=sqrt{x}\">"
      The image corresponding to a formula is created only once. Then the image is stocked into the image directories.
      The first time that mathfilter is called, the images corresponding to the formulas are created, but the next times mathfilter will only return the html code.

      NOTE : if the free latex fonts furnished with this script don't work well (very tiny formulas - that's could happened with some GD configurations), you should try to use the bakoma versions of these fonts (downloadable here : http://www.ctan.org/tex-archive/fonts/cm/ps-type1/bakoma/ttf/ )
     * ***************************************************************** */

//********* PARAMETERS TO MODIFY *********************************
// The four global variables. Uncomment the line if you need it.
//global $dirfonts,$dirimg,$symboles,$fontesmath;
// choose the type of the declaration according to your server settings (some servers don't accept the dirname(__FILE__) command for security reasons).
// NEW in 0.3 version : no more / at the end of $dirfonts and $dirimg
// absolute path to the fonts directory
    public $dirfonts = "/w/fonts";
// or $dirfonts=dirname(__FILE__)."/phpmathpublisher/fonts";
// absolute path to the images directory
    public $dirimg = "/w/img/equation";
// or $dirimg=dirname(__FILE__)."/phpmathpublisher/img";
//******************************************************************
    public $symboles = array(
        '~' => ' ',
        'alpha' => '&#174;',
        'beta' => '&#175;',
        'gamma' => '&#176;',
        'delta' => '&#177;',
        'epsilon' => '&#178;',
        'varepsilon' => '&#34;',
        'zeta' => '&#179;',
        'eta' => '&#180;',
        'theta' => '&#181;',
        'vartheta' => '&#35;',
        'iota' => '&#182;',
        'kappa' => '&#183;',
        'lambda' => '&#184;',
        'mu' => '&#185;',
        'nu' => '&#186;',
        'xi' => '&#187;',
        'pi' => '&#188;',
        'varpi' => '&#36;',
        'rho' => '&#189;',
        'varrho' => '&#37;',
        'sigma' => '&#190;',
        'varsigma' => '&#38;',
        'tau' => '&#191;',
        'upsilon' => '&#192;',
        'phi' => '&#193;',
        'varphi' => '&#39;',
        'chi' => '&#194;',
        'psi' => '&#195;',
        'omega' => '&#33;',
        'Gamma' => '&#161;',
        'Lambda' => '&#164;',
        'Sigma' => '&#167;',
        'Psi' => '&#170;',
        'Delta' => '&#162;',
        'Xi' => '&#165;',
        'Upsilon' => '&#168;',
        'Omega' => '&#173;',
        'Theta' => '&#163;',
        'Pi' => '&#166;',
        'Phi' => '&#169;',
        'infty' => '&#8734;',
        'ne' => '&#8800;',
        '*' => '&#215;',
        'in' => '&#8712;',
        'notin' => '&#8713;',
        'forall' => '&#8704;',
        'exists' => '&#8707;',
        'notexists' => '&#8708;',
        'partial' => '&#8706;',
        'approx' => '&#8776;',
        'left' => '&#8592;',
        'right' => '&#8594;',
        'leftright' => '&#8596;',
        'doubleleft' => '&#8656;',
        'doubleright' => '&#8658;',
        'doubleleftright' => '&#8660;',
        'nearrow' => '&#8599;',
        'searrow' => '&#8601;',
        'pm' => '&#177;',
        'bbR' => '&#8477;',
        'bbN' => '&#8469;',
        'bbZ' => '&#8484;',
        'bbC' => '&#8450;',
        'inter' => '&#8898;',
        'union' => '&#8899;',
        'ortho' => '&#8869;',
        'parallel' => '&#8741;',
        'backslash' => '&#92;',
        'prime' => '&#39;',
        'wedge' => '&#8896;',
        'vert' => '&#8741;',
        'subset' => '&#8834;',
        'notsubset' => '&#8836;',
        'circ' => '&#8728;',
        'varnothing' => '&#248;',
        'cdots' => '&#8943;',
        'vdots' => '&#8942;',
        'ddots' => '&#8945;',
//operateurs
        'le' => '&#54;',
        'ge' => '&#62;',
        '<' => '&#60;',
        '>' => '&#62;',
//parentheses
        '(' => '&#179;',
        ')' => '&#180;',
        '[' => '&#104;',
        ']' => '&#105;',
        'lbrace' => '&#40;',
        'rbrace' => '&#41;',
//autres
        '_hat' => '&#99;',
        '_racine' => '&#113;',
        '_integrale' => '&#82;',
        '_dintegrale' => '&#8748;',
        '_tintegrale' => '&#8749;',
        '_ointegrale' => '&#72;',
        '_produit' => '&#81;',
        '_somme' => '&#80;',
        '_intersection' => '&#84;',
        '_reunion' => '&#83;',
        '_lim' => 'lim',
//fonctions
        'arccos' => 'arccos',
        'ker' => 'ker',
        'arcsin' => 'arcsin',
        'lg' => 'lg',
        'arctan' => 'arctan',
        'arg' => 'arg',
        'cos' => 'cos',
        'cosh' => 'cosh',
        'ln' => 'ln',
        'cot' => 'cot',
        'log' => 'log',
        'coth' => 'coth',
        'max' => 'max',
        'csc' => 'csc',
        'min' => 'min',
        'deg' => 'deg',
        'det' => 'det',
        'sec' => 'sec',
        'dim' => 'dim',
        'sin' => 'sin',
        'exp' => 'exp',
        'sinh' => 'sinh',
        'gcd' => 'gcd',
        'sup' => 'sup',
        'hom' => 'hom',
        'tan' => 'tan',
        'inf' => 'inf',
        'tanh' => 'tanh'
    );
    public $fontesmath = array(
        '~' => 'FreeSerif',
        'alpha' => 'cmmi10',
        'beta' => 'cmmi10',
        'gamma' => 'cmmi10',
        'delta' => 'cmmi10',
        'epsilon' => 'cmmi10',
        'varepsilon' => 'cmmi10',
        'zeta' => 'cmmi10',
        'eta' => 'cmmi10',
        'theta' => 'cmmi10',
        'vartheta' => 'cmmi10',
        'iota' => 'cmmi10',
        'kappa' => 'cmmi10',
        'lambda' => 'cmmi10',
        'mu' => 'cmmi10',
        'nu' => 'cmmi10',
        'xi' => 'cmmi10',
        'pi' => 'cmmi10',
        'varpi' => 'cmmi10',
        'rho' => 'cmmi10',
        'varrho' => 'cmmi10',
        'sigma' => 'cmmi10',
        'varsigma' => 'cmmi10',
        'tau' => 'cmmi10',
        'upsilon' => 'cmmi10',
        'phi' => 'cmmi10',
        'varphi' => 'cmmi10',
        'chi' => 'cmmi10',
        'psi' => 'cmmi10',
        'omega' => 'cmmi10',
        'Gamma' => 'cmr10',
        'Lambda' => 'cmr10',
        'Sigma' => 'cmr10',
        'Psi' => 'cmr10',
        'Delta' => 'cmr10',
        'Xi' => 'cmr10',
        'Upsilon' => 'cmr10',
        'Omega' => 'cmr10',
        'Theta' => 'cmr10',
        'Pi' => 'cmr10',
        'Phi' => 'cmr10',
        'infty' => 'FreeSerif',
        'ne' => 'FreeSerif',
        '*' => 'FreeSerif',
        'in' => 'FreeSerif',
        'notin' => 'FreeSerif',
        'forall' => 'FreeSerif',
        'exists' => 'FreeSerif',
        'notexists' => 'FreeSerif',
        'partial' => 'FreeSerif',
        'approx' => 'FreeSerif',
        'left' => 'FreeSerif',
        'right' => 'FreeSerif',
        'leftright' => 'FreeSerif',
        'doubleleft' => 'FreeSerif',
        'doubleright' => 'FreeSerif',
        'doubleleftright' => 'FreeSerif',
        'nearrow' => 'FreeSerif',
        'searrow' => 'FreeSerif',
        'pm' => 'FreeSerif',
        'bbR' => 'FreeSerif',
        'bbN' => 'FreeSerif',
        'bbZ' => 'FreeSerif',
        'bbC' => 'FreeSerif',
        'inter' => 'FreeSerif',
        'union' => 'FreeSerif',
        'ortho' => 'FreeSerif',
        'parallel' => 'FreeSerif',
        'backslash' => 'FreeSerif',
        'prime' => 'FreeSerif',
        'wedge' => 'FreeSerif',
        'vert' => 'FreeSerif',
        'subset' => 'FreeSerif',
        'notsubset' => 'FreeSerif',
        'circ' => 'FreeSerif',
        'varnothing' => 'FreeSerif',
        'cdots' => 'FreeSerif',
        'vdots' => 'FreeSerif',
        'ddots' => 'FreeSerif',
//operateurs
        'le' => 'msam10',
        'ge' => 'msam10',
        '<' => 'cmmi10',
        '>' => 'cmmi10',
//parentheses
        '(' => 'cmex10',
        ')' => 'cmex10',
        '[' => 'cmex10',
        ']' => 'cmex10',
        'lbrace' => 'cmex10',
        'rbrace' => 'cmex10',
//autres
        '_hat' => 'cmex10',
        '_racine' => 'cmex10',
        '_integrale' => 'cmex10',
        '_dintegrale' => 'FreeSerif',
        '_tintegrale' => 'FreeSerif',
        '_ointegrale' => 'cmex10',
        '_produit' => 'cmex10',
        '_somme' => 'cmex10',
        '_intersection' => 'cmex10',
        '_reunion' => 'cmex10',
        '_lim' => 'cmr10',
//fonctions
        'arccos' => 'cmr10',
        'ker' => 'cmr10',
        'arcsin' => 'cmr10',
        'lg' => 'cmr10',
        'arctan' => 'cmr10',
        'arg' => 'cmr10',
        'cos' => 'cmr10',
        'cosh' => 'cmr10',
        'ln' => 'cmr10',
        'cot' => 'cmr10',
        'log' => 'cmr10',
        'coth' => 'cmr10',
        'max' => 'cmr10',
        'csc' => 'cmr10',
        'min' => 'cmr10',
        'deg' => 'cmr10',
        'det' => 'cmr10',
        'sec' => 'cmr10',
        'dim' => 'cmr10',
        'sin' => 'cmr10',
        'exp' => 'cmr10',
        'sinh' => 'cmr10',
        'gcd' => 'cmr10',
        'sup' => 'cmr10',
        'hom' => 'cmr10',
        'tan' => 'cmr10',
        'inf' => 'cmr10',
        'tanh' => 'cmr10'
    );

    public function __construct() {
        
    }

//    
//    public function MathBase() {
//        //
//    }

    public function est_nombre($str) {
        return preg_match("/^[0-9]/", $str);
    }

    public function tableau_expression($expression) {
        $e = str_replace('_', ' _ ', $expression);
        $e = str_replace('{(}', '{ }', $e);
        $e = str_replace('{)}', '{ }', $e);
        $t = token_get_all("<?php \$formula=$e ?" . ">");
        $extraits = array();
        $result = array();
//stupid code but token_get_all bug in some php versions
        $d = 0;
        for ($i = 0; $i < count($t); $i++) {
            if (is_array($t[$i]))
                $t[$i] = $t[$i][1];
            if (preg_match("/formula/", $t[$i])) {
                $d = $i + 2;
                break;
            }
        }
        for ($i = $d; $i < count($t) - 1; $i++) {
            if (is_array($t[$i]))
                $t[$i] = $t[$i][1];
            if ($t[$i] == '<=')
                $t[$i] = 'le';
            elseif ($t[$i] == '!=')
                $t[$i] = 'ne';
            elseif ($t[$i] == '<>')
                $t[$i] = 'ne';
            elseif ($t[$i] == '>=')
                $t[$i] = 'ge';
            elseif ($t[$i] == '--') {
                $t[$i] = '-';
                $t[$i + 1] = '-' . $t[$i + 1];
            } elseif ($t[$i] == '++')
                $t[$i] = '+';
            elseif ($t[$i] == '-') {
                if ($t[$i - 1] == '^' || $t[$i - 1] == '_' || $t[$i - 1] == '*' || $t[$i - 1] == '/' || $t[$i - 1] == '+' || $t[$i - 1] == '(') {
                    $t[$i] = '';
                    if (is_array($t[$i + 1]))
                        $t[$i + 1][1] = '-' . $t[$i + 1][1];
                    else
                        $t[$i + 1] = '-' . $t[$i + 1];
                }
            }
            if (trim($t[$i]) != '')
                $extraits[] = $t[$i];
        }
        for ($i = 0; $i < count($extraits); $i++) {
            $result[] = new ExpressionTexte($extraits[$i]);
        }
        return $result;
    }

// ugly hack, but GD is not very good with truetype fonts (especially with latex fonts)
    public function affiche_symbol($texte, $haut) {
        $texte = trim(stripslashes($texte));
        switch ($texte) {
            case '':
                $img = ImageCreate(1, max($haut, 1));
                $blanc = ImageColorAllocate($img, 255, 255, 255);
                $blanc = imagecolortransparent($img, $blanc);
                ImageFilledRectangle($img, 0, 0, 1, $haut, $blanc);
                break;
            case '~':
                $img = ImageCreate(1, max($haut, 1));
                $blanc = ImageColorAllocate($img, 255, 255, 255);
                $blanc = imagecolortransparent($img, $blanc);
                ImageFilledRectangle($img, 0, 0, 1, $haut, $blanc);
                break;
            case 'vert':
                $img = ImageCreate(6, max($haut, 1));
                $blanc = ImageColorAllocate($img, 255, 255, 255);
                $blanc = imagecolortransparent($img, $blanc);
                $noir = ImageColorAllocate($img, 0, 0, 0);
                ImageFilledRectangle($img, 0, 0, 6, $haut, $blanc);
                ImageFilledRectangle($img, 2, 0, 2, $haut, $noir);
                ImageFilledRectangle($img, 4, 0, 4, $haut, $noir);
                break;
            case '|':
                $img = ImageCreate(5, max($haut, 1));
                $blanc = ImageColorAllocate($img, 255, 255, 255);
                $blanc = imagecolortransparent($img, $blanc);
                $noir = ImageColorAllocate($img, 0, 0, 0);
                ImageFilledRectangle($img, 0, 0, 5, $haut, $blanc);
                ImageFilledRectangle($img, 2, 0, 2, $haut, $noir);
                break;
            case 'right':
                $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/" . $this->fontesmath[$texte] . ".ttf";
                $t = 16;
                $texte = $this->symboles[$texte];
                $tmp_dim = ImageTTFBBox($t, 0, $font, $texte);
                $tmp_largeur = abs($tmp_dim[2] - $tmp_dim[0]) + 2;
                $tmp_hauteur = abs($tmp_dim[3] - $tmp_dim[5]) + 2;
                $tmp_img = ImageCreate(max($tmp_largeur, 1), max($tmp_hauteur, 1));
                $tmp_noir = ImageColorAllocate($tmp_img, 0, 0, 0);
                $tmp_blanc = ImageColorAllocate($tmp_img, 255, 255, 255);
                $tmp_blanc = imagecolortransparent($tmp_img, $tmp_blanc);
                ImageFilledRectangle($tmp_img, 0, 0, $tmp_largeur, $tmp_hauteur, $tmp_blanc);
                ImageTTFText($tmp_img, $t, 0, 0, $tmp_hauteur, $tmp_noir, $font, $texte);
                $toutblanc = true;
                $sx = $sy = $ex = $ey = -1;
                for ($y = 0; $y < $tmp_hauteur; $y++) {
                    for ($x = 0; $x < $tmp_largeur; $x++) {
                        $rgb = ImageColorAt($tmp_img, $x, $y);
                        if ($rgb != $tmp_blanc) {
                            $toutblanc = false;
                            if ($sy == -1)
                                $sy = $y;
                            else
                                $ey = $y;

                            if ($sx == -1)
                                $sx = $x;
                            else {
                                if ($x < $sx)
                                    $sx = $x;
                                else if ($x > $ex)
                                    $ex = $x;
                            }
                        }
                    }
                }
                $nx = abs($ex - $sx);
                $ny = abs($ey - $sy);
                $img = ImageCreate(max($nx + 4, 1), max($ny + 4, 1));
                $blanc = ImageColorAllocate($img, 255, 255, 255);
                $blanc = imagecolortransparent($img, $blanc);
                ImageFilledRectangle($img, 0, 0, $nx + 4, $ny + 4, $blanc);
                ImageCopy($img, $tmp_img, 2, 2, $sx, $sy, min($nx + 2, $tmp_largeur - $sx), min($ny + 2, $tmp_hauteur - $sy));
                break;
            case '_hat':
                $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/" . $this->fontesmath[$texte] . ".ttf";
                $t = $haut;
                $texte = $this->symboles[$texte];
                $tmp_dim = ImageTTFBBox($t, 0, $font, $texte);
                $tmp_largeur = abs($tmp_dim[2] - $tmp_dim[0]);
                $tmp_hauteur = abs($tmp_dim[3] - $tmp_dim[5]) * 4;
                $tmp_img = ImageCreate(max($tmp_largeur, 1), max($tmp_hauteur, 1));
                $tmp_noir = ImageColorAllocate($tmp_img, 0, 0, 0);
                $tmp_blanc = ImageColorAllocate($tmp_img, 255, 255, 255);
                $tmp_blanc = imagecolortransparent($tmp_img, $tmp_blanc);
                ImageFilledRectangle($tmp_img, 0, 0, $tmp_largeur, $tmp_hauteur, $tmp_blanc);
                ImageTTFText($tmp_img, $t, 0, 0, $tmp_hauteur, $tmp_noir, $font, $texte);
                $toutblanc = true;
                $img = $tmp_img;
                $sx = $sy = $ex = $ey = -1;
                for ($y = 0; $y < $tmp_hauteur; $y++) {
                    for ($x = 0; $x < $tmp_largeur; $x++) {
                        $rgb = ImageColorAt($tmp_img, $x, $y);
                        if ($rgb != $tmp_blanc) {
                            $toutblanc = false;
                            if ($sy == -1)
                                $sy = $y;
                            else
                                $ey = $y;

                            if ($sx == -1)
                                $sx = $x;
                            else {
                                if ($x < $sx)
                                    $sx = $x;
                                else if ($x > $ex)
                                    $ex = $x;
                            }
                        }
                    }
                }
                $nx = abs($ex - $sx);
                $ny = abs($ey - $sy);
                $img = ImageCreate(max($nx + 4, 1), max($ny + 4, 1));
                $blanc = ImageColorAllocate($img, 255, 255, 255);
                $blanc = imagecolortransparent($img, $blanc);
                ImageFilledRectangle($img, 0, 0, $nx + 4, $ny + 4, $blanc);
                ImageCopy($img, $tmp_img, 2, 2, $sx, $sy, min($nx + 2, $tmp_largeur - $sx), min($ny + 2, $tmp_hauteur - $sy));
                break;
            case '_dintegrale':
            case '_tintegrale':
                if (isset($this->fontesmath[$texte]))
                    $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/" . $this->fontesmath[$texte] . ".ttf";
                elseif (self::est_nombre($texte))
                    $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/cmr10.ttf";
                else
                    $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/cmmi10.ttf";
                $t = 6;
                if (isset($this->symboles[$texte]))
                    $texte = $this->symboles[$texte];
                do {
                    $tmp_dim = ImageTTFBBox($t, 0, $font, $texte);
                    $t+=1;
                } while ((abs($tmp_dim[3] - $tmp_dim[5]) < 1.2 * $haut));
                $tmp_largeur = abs($tmp_dim[2] - $tmp_dim[0]) * 2;
                $tmp_hauteur = abs($tmp_dim[3] - $tmp_dim[5]) * 2;
                $tmp_img = ImageCreate(max($tmp_largeur, 1), max($tmp_hauteur, 1));
                $tmp_noir = ImageColorAllocate($tmp_img, 0, 0, 0);
                $tmp_blanc = ImageColorAllocate($tmp_img, 255, 255, 255);
                $tmp_blanc = imagecolortransparent($tmp_img, $tmp_blanc);
                ImageFilledRectangle($tmp_img, 0, 0, $tmp_largeur, $tmp_hauteur, $tmp_blanc);
                ImageTTFText($tmp_img, $t, 0, 5, $tmp_hauteur / 2, $tmp_noir, $font, $texte);
                $img = $tmp_img;
                $toutblanc = true;
                $sx = $sy = $ex = $ey = -1;
                for ($y = 0; $y < $tmp_hauteur; $y++) {
                    for ($x = 0; $x < $tmp_largeur; $x++) {
                        $rgb = ImageColorAt($tmp_img, $x, $y);
                        if ($rgb != $tmp_blanc) {
                            $toutblanc = false;
                            if ($sy == -1)
                                $sy = $y;
                            else
                                $ey = $y;

                            if ($sx == -1)
                                $sx = $x;
                            else {
                                if ($x < $sx)
                                    $sx = $x;
                                else if ($x > $ex)
                                    $ex = $x;
                            }
                        }
                    }
                }
                $nx = abs($ex - $sx);
                $ny = abs($ey - $sy);
                if ($toutblanc) {
                    $img = ImageCreate(1, max($haut, 1));
                    $blanc = ImageColorAllocate($img, 255, 255, 255);
                    $blanc = imagecolortransparent($img, $blanc);
                    ImageFilledRectangle($img, 0, 0, 1, $haut, $blanc);
                } else {
                    $img = ImageCreate(max($nx + 4, 1), max($ny + 4, 1));
                    $blanc = ImageColorAllocate($img, 255, 255, 255);
                    $blanc = imagecolortransparent($img, $blanc);
                    ImageFilledRectangle($img, 0, 0, $nx + 4, $ny + 4, $blanc);
                    ImageCopy($img, $tmp_img, 2, 2, $sx, $sy, min($nx + 2, $tmp_largeur - $sx), min($ny + 2, $tmp_hauteur - $sy));
                }
                break;
            default:
                if (isset($this->fontesmath[$texte]))
                    $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/" . $this->fontesmath[$texte] . ".ttf";
                elseif (self::est_nombre($texte))
                    $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/cmr10.ttf";
                else
                    $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/cmmi10.ttf";
                $t = 6;
                if (isset($this->symboles[$texte]))
                    $texte = $this->symboles[$texte];
                do {
                    $tmp_dim = ImageTTFBBox($t, 0, $font, $texte);
                    $t+=1;
                } while ((abs($tmp_dim[3] - $tmp_dim[5]) < $haut));
                $tmp_largeur = abs($tmp_dim[2] - $tmp_dim[0]) * 2;
                $tmp_hauteur = abs($tmp_dim[3] - $tmp_dim[5]) * 2;
                $tmp_img = ImageCreate(max($tmp_largeur, 1), max($tmp_hauteur, 1));
                $tmp_noir = ImageColorAllocate($tmp_img, 0, 0, 0);
                $tmp_blanc = ImageColorAllocate($tmp_img, 255, 255, 255);
                $tmp_blanc = imagecolortransparent($tmp_img, $tmp_blanc);
                ImageFilledRectangle($tmp_img, 0, 0, $tmp_largeur, $tmp_hauteur, $tmp_blanc);
                ImageTTFText($tmp_img, $t, 0, 0, $tmp_hauteur / 4, $tmp_noir, $font, $texte);
// 	ImageTTFText($tmp_img, $t, 0,5,5,$tmp_noir, $font,$texte);
//	$img=$tmp_img;
                $toutblanc = true;
                $sx = $sy = $ex = $ey = -1;
                for ($y = 0; $y < $tmp_hauteur; $y++) {
                    for ($x = 0; $x < $tmp_largeur; $x++) {
                        $rgb = ImageColorAt($tmp_img, $x, $y);
                        if ($rgb != $tmp_blanc) {
                            $toutblanc = false;
                            if ($sy == -1)
                                $sy = $y;
                            else
                                $ey = $y;

                            if ($sx == -1)
                                $sx = $x;
                            else {
                                if ($x < $sx)
                                    $sx = $x;
                                else if ($x > $ex)
                                    $ex = $x;
                            }
                        }
                    }
                }
                $nx = abs($ex - $sx);
                $ny = abs($ey - $sy);
                if ($toutblanc) {
                    $img = ImageCreate(1, max($haut, 1));
                    $blanc = ImageColorAllocate($img, 255, 255, 255);
                    $blanc = imagecolortransparent($img, $blanc);
                    ImageFilledRectangle($img, 0, 0, 1, $haut, $blanc);
                } else {
                    $img = ImageCreate(max($nx + 4, 1), max($ny + 4, 1));
                    $blanc = ImageColorAllocate($img, 255, 255, 255);
                    $blanc = imagecolortransparent($img, $blanc);
                    ImageFilledRectangle($img, 0, 0, $nx + 4, $ny + 4, $blanc);
                    ImageCopy($img, $tmp_img, 2, 2, $sx, $sy, min($nx + 2, $tmp_largeur - $sx), min($ny + 2, $tmp_hauteur - $sy));
                }
                break;
        }
//$rouge=ImageColorAllocate($img,255,0,0);
//ImageRectangle($img,0,0,ImageSX($img)-1,ImageSY($img)-1,$rouge);
        return $img;
    }

    public function affiche_texte($texte, $taille) {
        $taille = max($taille, 6);
        $texte = stripslashes($texte);
        $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/cmr10.ttf";
        $htexte = 'dg' . $texte;
        $hdim = ImageTTFBBox($taille, 0, $font, $htexte);
        $wdim = ImageTTFBBox($taille, 0, $font, $texte);
        $dx = max($wdim[2], $wdim[4]) - min($wdim[0], $wdim[6]) + ceil($taille / 8);
        $dy = max($hdim[1], $hdim[3]) - min($hdim[5], $hdim[7]) + ceil($taille / 8);
        $img = ImageCreate(max($dx, 1), max($dy, 1));
        $noir = ImageColorAllocate($img, 0, 0, 0);
        $blanc = ImageColorAllocate($img, 255, 255, 255);
        $blanc = imagecolortransparent($img, $blanc);
        ImageFilledRectangle($img, 0, 0, $dx, $dy, $blanc);
//ImageRectangle($img,0,0,$dx-1,$dy-1,$noir);
        ImageTTFText($img, $taille, $angle, 0, -min($hdim[5], $hdim[7]), $noir, $font, $texte);
        return $img;
    }

    public function affiche_math($texte, $taille) {
        $taille = max($taille, 6);
        $texte = stripslashes($texte);
        if (isset($this->fontesmath[$texte]))
            $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/" . $this->fontesmath[$texte] . ".ttf";
        elseif (preg_match("/[a-zA-Z]/", $texte))
            $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/cmmi10.ttf";
        else
            $font = $_SERVER['DOCUMENT_ROOT'] . $this->dirfonts . "/cmr10.ttf";
        if (isset($this->symboles[$texte]))
            $texte = $this->symboles[$texte];
        $htexte = 'dg' . $texte;
        $hdim = ImageTTFBBox($taille, 0, $font, $htexte);
        $wdim = ImageTTFBBox($taille, 0, $font, $texte);
        $dx = max($wdim[2], $wdim[4]) - min($wdim[0], $wdim[6]) + ceil($taille / 8);
        $dy = max($hdim[1], $hdim[3]) - min($hdim[5], $hdim[7]) + ceil($taille / 8);
        $img = ImageCreate(max($dx, 1), max($dy, 1));
        $noir = ImageColorAllocate($img, 0, 0, 0);
        $blanc = ImageColorAllocate($img, 255, 255, 255);
        $blanc = imagecolortransparent($img, $blanc);
        ImageFilledRectangle($img, 0, 0, $dx, $dy, $blanc);
//ImageRectangle($img,0,0,$dx-1,$dy-1,$noir);
        ImageTTFText($img, $taille, 0, 0, -min($hdim[5], $hdim[7]), $noir, $font, $texte);
        return $img;
    }

    public function parenthese($hauteur, $style) {
        $image = self::affiche_symbol($style, $hauteur);
        return $image;
    }

    public function alignement2($image1, $base1, $image2, $base2) {
        $largeur1 = imagesx($image1);
        $hauteur1 = imagesy($image1);
        $largeur2 = imagesx($image2);
        $hauteur2 = imagesy($image2);
        $dessus = max($base1, $base2);
        $dessous = max($hauteur1 - $base1, $hauteur2 - $base2);
        $largeur = $largeur1 + $largeur2;
        $hauteur = $dessus + $dessous;
        $result = ImageCreate(max($largeur, 1), max($hauteur, 1));
        $noir = ImageColorAllocate($result, 0, 0, 0);
        $blanc = ImageColorAllocate($result, 255, 255, 255);
        $blanc = imagecolortransparent($result, $blanc);
        ImageFilledRectangle($result, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
        ImageCopy($result, $image1, 0, $dessus - $base1, 0, 0, $largeur1, $hauteur1);
        ImageCopy($result, $image2, $largeur1, $dessus - $base2, 0, 0, $largeur2, $hauteur2);
//ImageRectangle($result,0,0,$largeur-1,$hauteur-1,$noir);
        return $result;
    }

    public function alignement3($image1, $base1, $image2, $base2, $image3, $base3) {
        $largeur1 = imagesx($image1);
        $hauteur1 = imagesy($image1);
        $largeur2 = imagesx($image2);
        $hauteur2 = imagesy($image2);
        $largeur3 = imagesx($image3);
        $hauteur3 = imagesy($image3);
        $dessus = max($base1, $base2, $base3);
        $dessous = max($hauteur1 - $base1, $hauteur2 - $base2, $hauteur3 - $base3);
        $largeur = $largeur1 + $largeur2 + $largeur3;
        $hauteur = $dessus + $dessous;
        $result = ImageCreate(max($largeur, 1), max($hauteur, 1));
        $noir = ImageColorAllocate($result, 0, 0, 0);
        $blanc = ImageColorAllocate($result, 255, 255, 255);
        $blanc = imagecolortransparent($result, $blanc);
        ImageFilledRectangle($result, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
        ImageCopy($result, $image1, 0, $dessus - $base1, 0, 0, $largeur1, $hauteur1);
        ImageCopy($result, $image2, $largeur1, $dessus - $base2, 0, 0, $largeur2, $hauteur2);
        ImageCopy($result, $image3, $largeur1 + $largeur2, $dessus - $base3, 0, 0, $largeur3, $hauteur3);
//ImageRectangle($result,0,0,$largeur-1,$hauteur-1,$noir);
        return $result;
    }

}

?>
